/*
CommandLineParser.cpp
Class assigned for parsing command line arguments into different classes.
*/


#include "CommandLineParser.h"

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#include <parsing/Lexer.h>

using namespace std;

void CommandLineParser :: parseToken(const std::string s) {
	if (s.length() >= 3) {
		if (s[0] == '-' && s[1] == '-') {
			size_t mid = s.find('=');
			if (mid != std::string::npos) {
				string key = s.substr(2, mid - 2);
				string value = s.substr(mid + 1);
				map.insert(std::pair<std::string, std::string>(key, value));
			} else {
				options.insert(s.substr(2));
			}
		} else if (s[0] == '-') {
			std::string key = s.substr(1, 1);
			std::string value = s.substr(2);
			map.insert(std::pair<std::string, std::string>(key, value));
		} else {
			args.push_back(s);
		}
	} else {
		if (s[0] == '-') {
			options.insert(s.substr(1));
		} else {
			args.push_back(s);
		}
	}
}

void CommandLineParser :: parse(int argc, const char** argv) {
	for (int i = 0; i < argc; i++) {
		parseToken(string(argv[i]));
	}
}

void CommandLineParser :: parse(const char* str) {
	Lexer lex(str);
	
	while(lex.hasNext()) {
		parseToken(lex.next());
	}
}


std::map<std::string, std::string>& CommandLineParser :: getMap() {
	return map;
}

std::set<std::string>& CommandLineParser :: getOptions() {
	return options;
}

std::vector<std::string>& CommandLineParser :: getArguments() {
	return args;
}

void CommandLineParser :: clear() {
	map.clear();
	args.clear();
	options.clear();
}

